package com.r_z.service.impl;

import com.r_z.entity.FiscalExpenditure;
import com.r_z.entity.Goods;
import com.r_z.entity.Purchase;
import com.r_z.entity.PurchaseDetails;
import com.r_z.mapper.PurchaseMapper;
import com.r_z.service.PurchaseService;
import com.r_z.util.CreateIDUtil;
import com.r_z.util.DateTimeUtil;
import com.r_z.util.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 周楠櫻木
 * @version 1.0
 * @date 2023/6/8
 */
@Service
public class PurchaseServiceImpl implements PurchaseService {
    @Resource
    PurchaseMapper purchaseMapper;
    @Override
    public List<Purchase> selectPurchase(String purchaseId, int page, int limit) {
        return null;
    }

    @Override
    public boolean insertPurchase(Goods[] goods) {
        String maxId = purchaseMapper.purchaseMaxId();
        String number = CreateIDUtil.createId(maxId,"P");
        String maxId2 = purchaseMapper.fExpenditureIdMax();
        String number2 = CreateIDUtil.createId(maxId2,"FE");
        //采购列表的id编号创建
        //String number = NumberUtil.getNumber("purchaseId", "purchase", "P");
        int num=1;//定义循环次数
        int purchaseNum=0;//定义采购列表总数量
        double money=0;//定义总金额
        boolean flag=false;
        for (Goods goodList : goods) {
            System.out.println("我踏马来了======"+goodList.toString());
            if (num<2) {
                //调用和采购列表的增加方法
                flag = purchaseMapper.insertPurchase(new Purchase(number, purchaseNum, goodList.getPrice(),
                        "2", 2, 3, DateTimeUtil.getSystemDateTime("yyyy-MM-dd"), 2,""));
            }
            //调用和采购详情列表的增加方法
            purchaseMapper.insertPurchaseDetails(new PurchaseDetails(goodList.getPrice(),
                    goodList.getQuantity(), goodList.getPrice()*goodList.getQuantity(), goodList.getGoodId(),number));
            num++;
            money = money+(goodList.getPrice()*goodList.getQuantity());
        }
        if (flag) {
            //调用采购列表的删除所有列方法
            flag = purchaseMapper.dropPurchasePlanAll();
        }
        //创建财务单号
        String fExpenditureId = NumberUtil.getNumber("fExpenditureId", "fiscalExpenditure", "FE");
        //调用财务支出表的增加方法
        purchaseMapper.insertFiscalExpenditure(new FiscalExpenditure(number2, number, money, DateTimeUtil.getSystemDateTime("yyyy-MM-dd"), 2, 2, ""));
        //遍历采购详情得到总数量
        List<PurchaseDetails> purchaseDetailsList = purchaseMapper.selectPurchaseDetails(number);
        int quantity=0;//定义采购列表的总数量
        double total=0;//定义采购列表的总金额
        for (PurchaseDetails purchaseDetails : purchaseDetailsList) {//循环详情集合将数量和金额加入定义的总计里
            quantity=quantity+purchaseDetails.getQuantity();
            total=total + purchaseDetails.getTotalDetails();
        }
        //调用采购列表的修改方法
        purchaseMapper.updatePurchase(quantity,number,total);
        return flag;
    }

    @Override
    public List<PurchaseDetails> selectPurchaseDetails(String purchaseId) {
        return null;
    }

    @Override
    public List<Purchase> selectPurchaseAll(String goodName) {
        return null;
    }

    @Override
    public List<PurchaseDetails> selectStatement(String purchaseId) {
        return null;
    }
}
